package controllers

import (
	"github.com/gin-gonic/gin"
	"math"
	"net/http"
	"strconv"
	"test/models"
	"test/system"
)

func IndexJson(c *gin.Context) {
	c.JSON(200, "json-xxxx")
}

//首页
func IndexGet(c *gin.Context) {

	var (
		page      string
		pageIndex int
		pageSize  = system.GetConfiguration().PageSize
		posts     []*models.Post
		err       error
		name      string
		total     int
	)
	page = c.Query("page")

	pageIndex, _ = strconv.Atoi(page)
	if pageIndex <= 0 {
		pageIndex = 1
	}

	name = "wbz"
	posts, err = models.ListPublishedPost("wbz", pageIndex, pageSize)
	//println(posts)

	if err != nil {
		println("error")
		return
	}
	total, err = models.CountPostByTag("")

	/*for i:=0; i<len(posts); i++ {
		println(posts[i])
	}*/
	c.HTML(http.StatusOK, "index/index.html", gin.H{
		"posts":     posts,
		"test":      "wbz-test",
		"name":      name,
		"totalPage": int(math.Ceil(float64(total) / float64(pageSize))),
		"pageIndex": pageIndex,
	})

}

func Test(c *gin.Context) {
	c.HTML(http.StatusOK, "index/welcome.html", gin.H{
		"title": "Welcome GO GIN",
	})
}
